﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data.SqlClient;
using Utility;
using System.Data;
namespace Service
{
    public class TShowPassWordService
    {
        public int Add(TShowPassWord entity,SqlTransaction trans)
        {
            string sql = "insert into TShowPassWord values(@username,@password)";
            SqlParameter[] sp=new SqlParameter[]{
                new SqlParameter("@username",entity.xx_UserName),
                new SqlParameter("@password",entity.xx_password)
            };
            int n= SqlHelper.ExecuteNonQuery(trans,CommandType.Text, sql, sp);
            return n;
        }
        public int Reset(SqlTransaction trans,string searchValue,string password)
        {
            string sql2 = "select * from TShowPassWord where xx_username=@searchValue";
            SqlParameter[] sp2 = new SqlParameter[]{
                new SqlParameter("@searchValue",searchValue)
            };
           var ds=SqlHelper.ExecuteDataset(Utility.WebConn.connString, CommandType.Text, sql2, sp2);
           if (ds.Tables[0].Rows.Count == 0)
           {
               TShowPassWord entity = new TShowPassWord()
               {
                   xx_UserName = searchValue,
                   xx_password = password
               };
               return Add(entity,trans);
           }
           else
           {
               string sql = @"update TShowPassWord set xx_password=@password where xx_username=@searchValue";
               SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@password",password),
                new SqlParameter("@searchValue",searchValue)
                };
               int n = SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sql, sp);
               return n;
           }
        }
        public int Delete(SqlTransaction trans, string skey)
        {
            string sql = "delete from TShowPassWord where xx_username=@skey";
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@skey",skey),
            };
            int n = SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sql, sp);
            return n;
        }
    }
}
